package com.qf.servlet;

import com.qf.pojo.PageBean;
import com.qf.pojo.Product;
import com.qf.service.ProductService;
import com.qf.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/product")
public class ProductServlet extends baseServlet {
    public void findProduct(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //获取参数pid
        String pid = request.getParameter("pid");
        //调用业务层
        ProductService  productService = new ProductServiceImpl();
        Product product = productService.findProductByid(pid);
        //将product存储在request中
        request.setAttribute("product",product);
        //请求转发到商品详情页面
        request.getRequestDispatcher("/jsp/product_info.jsp").forward(request,response);
    }

    public void findProductByPage(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //获取参数
        String curPage = request.getParameter("currentPage");//当前页码
        String pSize = request.getParameter("pageSize");
        String cid = request.getParameter("cid");
        //类型转换
        int currentPage = Integer.parseInt(curPage);
        int pageSize = Integer.parseInt(pSize);
        //调用业务层
        ProductService  productService = new ProductServiceImpl();

        PageBean<Product> pageBean = productService.findProductByPage(currentPage, pageSize, cid);
        pageBean.setTotalPage(pageBean.getTotalPage());
        //请求转发到product_list.jsp中
        request.setAttribute("pb",pageBean);
        request.getRequestDispatcher("/jsp/product_list.jsp").forward(request,response);


    }

}
